package com.administrator.cloudapplication.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.administrator.cloudapplication.Interface.ListItemClickHelp;
import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.constant.Urls;
import com.administrator.cloudapplication.models.EbookModel;
import com.administrator.cloudapplication.models.MediaModel;
import com.administrator.cloudapplication.tools.ViewUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/18.
 */

public class VideoGridViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<MediaModel.DataBean.VideolistBean> mListType = new ArrayList<>();
   private List<EbookModel.EbookDataBean.BooklistBean> mEbookDataBean = new ArrayList<>();
    private ListItemClickHelp callback;
    private int type;
    String str;

    public VideoGridViewAdapter(Activity activity, List<MediaModel.DataBean.VideolistBean> mVideolistBean,
                                List<EbookModel.EbookDataBean.BooklistBean> mEbookDataBean,int type,
                                ListItemClickHelp callback) {
        mContext = activity;
        mListType = mVideolistBean;
        this.type=type;
        this.mEbookDataBean=mEbookDataBean;
        mInflater = LayoutInflater.from(activity);
        this.callback=callback;
    }

    @Override
    public int getCount() {
        switch (type) {
            case 0:
                return mListType.size();
            case 1:
                return mEbookDataBean.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        switch (type) {
            case 0:
                return mListType.get(i);
            case 1:
                return mEbookDataBean.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder {
        ImageView cover;
        TextView viname;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.video_grid_item, null);
            holder = new ViewHolder();
            holder.cover = (ImageView) view.findViewById(R.id.video_grid_img);
            holder.viname = (TextView) view.findViewById(R.id.video_grid_tv);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        ViewGroup.LayoutParams params = holder.cover.getLayoutParams();
        int width = ViewUtils.getScreenWidth((Activity) mContext);
        if(type==0){
            params.width = width / 2 - ViewUtils.dp2px(mContext, 20);
            params.height = (int) (params.width * 0.75);
            holder.cover.setLayoutParams(params);
            MediaModel.DataBean.VideolistBean  mVideoModel = (MediaModel.DataBean.VideolistBean) getItem(position);
            str = Urls.IMG_URL2 + mVideoModel.getVithumb();
            Glide.with(mContext).load(str).placeholder(R.mipmap.error).error(R.mipmap.error).into(holder.cover);
            holder.viname.setText(mVideoModel.getViname());
            holder.cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    callback.onImgClick(mListType,null,position);
                }
            });
        }
        else {
            params.width = width / 3 - ViewUtils.dp2px(mContext, 20);
            params.height = (int) (params.width * 1.6);
            holder.cover.setLayoutParams(params);
            Glide.with(mContext).load(Urls.IMG_URL2 + mEbookDataBean.get(position).getBookthumb())
                    .placeholder(R.mipmap.error).error(R.mipmap
                    .error).into(holder.cover);
            holder.viname.setText(mEbookDataBean.get(position).getBookname());
            holder.cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    callback.onImgClick(null,mEbookDataBean,position);
                }
            });
        }

        return view;
    }
}
